package com.szxy.example;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.style.TableStyle;
import com.szxy.domain.ExternalElectricityMarketAnalysisReportData;
import com.szxy.plugin.OneCommonTablePolicy;
import com.szxy.plugin.TwoCommonTablePolicy;
import com.szxy.poitl.PoitlMain;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Author: zwz
 * @Date:2020/7/28 14:59
 * @Description:com.szxy.example
 * @Version:1.0 电力市场分析报告（外部）版
 **/
public class ExternalElectrictyMarketAnalysisReportMain {


    /**
     * 文件名
     */
    public static final String FILE_NAME = "HuibeiElectricReportExternal.docx";
    /**
     * 文件完整路径d
     */
    public static final String path = PoitlMain.class.getClassLoader().getResource(FILE_NAME).getPath();

    /**
     * 数据
     */
    public static ExternalElectricityMarketAnalysisReportData datas = new ExternalElectricityMarketAnalysisReportData();

    /**
     * 行样式
     */
    public static TableStyle rowStyle = new TableStyle();

    /**
     * 数据准备
     */
    public static void before() {
        // 模拟从数据库取出的数据
        Map<String, Object> map18 = getDataFromDb(18);
        Map<String, Object> map72 = getDataFromDb(72);
        Map<String, Object> map60 = getDataFromDb(66);

        // 全社会
        List<RowRenderData> allSocialList = getRowRenderData(map18, 4);
        // 淮北大客户
        List<RowRenderData> bigUserList = getRowRenderData2(map72, 5);
        // 工业企业
        List<RowRenderData> industryList = getRowRenderData3(map60, 9);


        datas.setAllSocialPowerData(allSocialList);
        datas.setHuibeiBigUserStatisticsData(bigUserList);
        datas.setHuibeiIndustryPowerStatisticsData(industryList);
    }

    /**
     * 第一行放空值
     * @param map
     * @param paramLength
     * @return
     */
    private static List<RowRenderData> getRowRenderData(Map<String, Object> map, int paramLength) {
        List<RowRenderData> allSocialList = new ArrayList<>();
        String[] params = new String[paramLength];
        params[0] = "";
        int index = 1;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = (String) entry.getValue();
            params[index++] = value;

            if (index == paramLength) {
                RowRenderData row = RowRenderData.build(params);
                allSocialList.add(row);
                index = 1;
            }
        }
        return allSocialList;
    }


    /**
     * 前二行放空值
     * @param map
     * @param paramLength
     * @return
     */
    private static List<RowRenderData> getRowRenderData2(Map<String, Object> map, int paramLength) {
        List<RowRenderData> allSocialList = new ArrayList<>();
        String[] params = new String[paramLength];
        params[0] = "";
        params[1] = "";
        int index = 2;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = (String) entry.getValue();
            params[index++] = value;

            if (index == paramLength) {
                RowRenderData row = RowRenderData.build(params);
                allSocialList.add(row);
                index = 2;
            }
        }
        return allSocialList;
    }

    /**
     * 特殊处理第一行和第二行
     * @param map  数据
     * @param paramLength 一行数据的长度
     * @return
     */
    private static List<RowRenderData> getRowRenderData3(Map<String, Object> map, int paramLength) {
        List<RowRenderData> allSocialList = new ArrayList<>();
        String[] params = new String[paramLength];
        params[0] = "";
        params[1] = "";
        int index = 2;
        int count = 0;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = (String) entry.getValue();
            params[index++] = value;
            count++;

            if(count > 8 && count < 56) {
                params[2] = "";
            }

            if (index == paramLength) {
                RowRenderData row = RowRenderData.build(params);
                allSocialList.add(row);
                if(count == 55) {
                    index = 2;
                }else {
                    index = 3;
                }
            }

        }
        return allSocialList;
    }


    /**
     * 初始化
     */
    public static void init() throws Exception {
        // 声明配置
        Configure config = Configure.newBuilder()
                .customPolicy("all_social", new OneCommonTablePolicy())
                .customPolicy("huibei_big_user_statistics", new TwoCommonTablePolicy())
                .customPolicy("huibei_industry_power_statistics", new TwoCommonTablePolicy())
                .build();
        // 核心 API
        XWPFTemplate template = XWPFTemplate.compile(path, config).render(datas);
        // 输出
        FileOutputStream out = new FileOutputStream("d:\\desktop\\external.docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) throws Exception {
        before();
        init();
    }


    /**
     * 生成模拟数据
     *
     * @return
     */
    private static Map<String, Object> getDataFromDb(int num) {
        // o1 : col_1,col_2,col_3,...,col_n
        Map<String, Object> map = new TreeMap<>((o1, o2) -> {
            String o11 = o1.substring(o1.indexOf("_") + 1);
            String o22 = o2.substring(o2.indexOf("_") + 1);
            return Integer.valueOf(o11).compareTo(Integer.valueOf(o22));
        });
        for (int i = 0; i < num; i++) {
            map.put(String.format("col_%s", i + 1), String.valueOf(i + 1));
        }
        return map;
    }


}
